/**
 * 语音识别测试工具函数
 * 可以在浏览器控制台直接调用这些函数来测试 ChatLab 功能
 */

// 模拟 ChatLab 对象
export const mockChatLab = () => {
  if (!(window as any).ChatLab) {
    (window as any).ChatLab = {
      wakeup: () => {
        console.log('🎤 模拟 ChatLab.wakeup() 被调用');
        return '语音唤醒功能已开启';
      },
      startRecognize: () => {
        console.log('🎤 模拟 ChatLab.startRecognize() 被调用');
        return '开始语音识别';
      },
      stopRecognize: () => {
        console.log('🎤 模拟 ChatLab.stopRecognize() 被调用');
        return '停止语音识别';
      }
    };
    console.log('✅ ChatLab 模拟对象已创建');
    return true;
  } else {
    console.log('⚠️ ChatLab 已存在');
    return false;
  }
};

// 检查 ChatLab 状态
export const checkChatLabStatus = () => {
  const hasChatLab = !!(window as any).ChatLab;
  const hasOnRecognizeWords = !!(window as any).onRecognizeWords;
  
  console.log('🔍 ChatLab 状态检查:', {
    hasChatLab,
    hasOnRecognizeWords,
    ChatLabMethods: hasChatLab ? Object.keys((window as any).ChatLab) : [],
  });
  
  return { hasChatLab, hasOnRecognizeWords };
};

// 模拟语音识别结果
export const simulateVoiceRecognition = (text: string = '你好，这是一个测试', type: 'wakeup' | 'recognize' = 'recognize') => {
  if (!(window as any).onRecognizeWords) {
    console.error('❌ onRecognizeWords 回调未设置');
    return false;
  }

  const result = { words: text, type };
  console.log('🎯 模拟语音识别结果:', result);
  
  // 调用 onRecognizeWords 回调
  (window as any).onRecognizeWords(result);
  return true;
};

// 模拟流式语音识别
export const simulateStreamingRecognition = (text: string = '你好，这是一个流式测试', type: 'wakeup' | 'recognize' = 'recognize', speed: number = 200) => {
  if (!(window as any).onRecognizeWords) {
    console.error('❌ onRecognizeWords 回调未设置');
    return false;
  }

  console.log('🎯 开始流式语音识别:', { text, type, speed });
  
  let currentText = '';
  const words = text.split('');
  let index = 0;

  const interval = setInterval(() => {
    if (index < words.length) {
      currentText += words[index];
      
      const result = { words: currentText, type };
      console.log('🎯 流式识别结果:', result);
      (window as any).onRecognizeWords(result);
      
      index++;
    } else {
      clearInterval(interval);
      console.log('✅ 流式识别完成');
    }
  }, speed);

  return interval;
};

// 测试 ChatLab 方法
export const testChatLabMethods = () => {
  if (!(window as any).ChatLab) {
    console.error('❌ ChatLab 不可用，请先调用 mockChatLab()');
    return false;
  }

  try {
    console.log('🧪 开始测试 ChatLab 方法...');
    
    // 测试 wakeup
    const wakeupResult = (window as any).ChatLab.wakeup();
    console.log('wakeup 结果:', wakeupResult);
    
    // 测试 startRecognize
    const startResult = (window as any).ChatLab.startRecognize();
    console.log('startRecognize 结果:', startResult);
    
    // 测试 stopRecognize
    const stopResult = (window as any).ChatLab.stopRecognize();
    console.log('stopRecognize 结果:', stopResult);
    
    console.log('✅ ChatLab 方法测试完成');
    return true;
  } catch (error) {
    console.error('❌ ChatLab 方法测试失败:', error);
    return false;
  }
};

// 完整的测试流程
export const runFullTest = () => {
  console.log('🚀 开始完整语音识别测试...');
  
  // 1. 检查状态
  const status = checkChatLabStatus();
  
  // 2. 如果没有 ChatLab，创建模拟对象
  if (!status.hasChatLab) {
    mockChatLab();
  }
  
  // 3. 测试 ChatLab 方法
  testChatLabMethods();
  
  // 4. 等待一秒后模拟语音识别
  setTimeout(() => {
    simulateVoiceRecognition('测试语音识别功能', 'recognize');
  }, 1000);
  
  // 5. 等待两秒后模拟流式识别
  setTimeout(() => {
    simulateStreamingRecognition('这是一个流式语音识别测试', 'recognize', 300);
  }, 3000);
  
  console.log('✅ 完整测试已启动，请查看控制台输出');
};

// 在全局对象上挂载这些函数，方便在控制台调用
if (typeof window !== 'undefined') {
  (window as any).VoiceTestUtils = {
    mockChatLab,
    checkChatLabStatus,
    simulateVoiceRecognition,
    simulateStreamingRecognition,
    testChatLabMethods,
    runFullTest
  };
  
  console.log('🎤 语音识别测试工具已加载到 window.VoiceTestUtils');
  console.log('💡 使用方法:');
  console.log('  - VoiceTestUtils.runFullTest() - 运行完整测试');
  console.log('  - VoiceTestUtils.mockChatLab() - 创建 ChatLab 模拟对象');
  console.log('  - VoiceTestUtils.simulateVoiceRecognition("测试文本") - 模拟语音识别');
  console.log('  - VoiceTestUtils.checkChatLabStatus() - 检查状态');
} 